package com.richman.controller;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.utils.SoundThread;

public class AutoBetCentralManager {

	
	private SendData betgame;
	private String bettype = "";
	private String updown = "";
	
	
	public void dobetgame(String bettypearg, String updownarg, SendData betgamearg) {
		try {
			
			
			bettype = bettypearg;
			updown = updownarg;

			betgame = betgamearg;
			
			
			String hgTeamlbStr = "";
			String ps38Teamlbstr = "";
			
			
			String ps38typedetail = "";
			
			String ps38betid = "";
			
			String wtypestr = "";
			
			String typestr = "";
			
			String gnumstr = "";
			
			String strongstr = "";
			
			String ps38betpankou = "";
			
			String gidstr = "";
			
			
			if(bettype.contains("半场")) {
				wtypestr = "H";	
				
				gidstr = betgame.hgevent.hgid;
				
				ps38betid = betgame.event.halfBetID;
				
			}else {
				
				gidstr = betgame.hgevent.gid;
				
				if(bettype.contains("大小")) {
					ps38betid = betgame.event.wholeDXPBetID;
				}else {
					ps38betid = betgame.event.wholeRQPBetID;
				}
				
			}
			
			
			//滚球大小
			if(bettype.contains("滚球") && bettype.contains("大小")) {
				wtypestr += "R";
			}
			

			
			if(bettype.contains("让球")) {
				
				wtypestr += "R";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.homeTeam + "@";
					ps38Teamlbstr = betgame.event.awayTeam + "@";
					ps38typedetail = "客";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
					
				}else {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.awayTeam + "@";
					ps38Teamlbstr = betgame.event.homeTeam + "@";
					ps38typedetail = "主";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;
				}
				
				
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholerqppankou;
					strongstr = betgame.hgevent.strong;
				}else {
					ps38betpankou = betgame.event.halfrqppankou;
					strongstr = betgame.hgevent.hstrong;
				}
				

				
			}else {
				
				wtypestr += "OU";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" +  "大@";
					ps38Teamlbstr = "小@";
					ps38typedetail = "小";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;

				}else {
					hgTeamlbStr = "皇冠:" + "小@";
					ps38Teamlbstr = "大@";
					ps38typedetail = "大";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
				}
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholeDXPPankou;
				}else {
					ps38betpankou = betgame.event.halfDXPPankou;
				}
				
				
			}
			

			//滚球让球
			if(bettype.contains("滚球") && bettype.contains("让球")) {
				wtypestr += "E";
			}
			
			
			
			
			
			
			
			
			
			Map<String, String> oddUpdres = BetClient.betCtrMng.updateRatio(gidstr, typestr, gnumstr, strongstr, wtypestr + typestr, wtypestr);
			
			Map<String, String> ps38oddUpdres = BetClient.betCtrMng.updatePS38Ratio(ps38betid, Long.toString(betgame.event.eventId), bettype, ps38typedetail, ps38betpankou);
			
			
			if(oddUpdres == null || ps38oddUpdres == null) {
				Common.logOut("AutoBetCentralManager dobetgame 更新赔率失败");
				return;
			}
			
			Double hgratio = Double.parseDouble(oddUpdres.get("ratio"));
			Double ps38ratio = Double.parseDouble(ps38oddUpdres.get("ratio"));
			
			
			Double hgpankou = Double.parseDouble(oddUpdres.get("pankou"));
			Double ps38pankou = Double.parseDouble(ps38oddUpdres.get("pankou"));
			
			
			if(Math.abs(hgpankou - ps38pankou) > 0.01) {				
				return;
			}
			
			
			
			
			int hgmaxbetgold = Integer.parseInt(oddUpdres.get("maxbet").replace(",", ""));
			
			int hgsetbetgold = BetClient.mainWnd.gethgBetAmount();
			
			
			
			int hgbetgold = 0;
			
			
			
			//double ps38betgold = DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
			
			
			
			
			double ps38betgold = 0.0;//DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
			
			if(Double.parseDouble(ps38oddUpdres.get("maxbet"))   < BetClient.mainWnd.getstartBetGold() ){
				Common.logOut("平博最大投注值小于起投标准");
				return;
			}
			
			
			
			if(BetClient.mainWnd.getbps38betAmount()== true) {
				ps38betgold = BetClient.mainWnd.getps38betAmount();
				
				if(hgsetbetgold < hgmaxbetgold) {
					hgbetgold = hgsetbetgold;
				}else {
					hgbetgold = hgmaxbetgold;
				}
				

				
				
			}else {
				
				ps38betgold = Double.parseDouble(ps38oddUpdres.get("maxbet"));
				
				hgbetgold = (int)(ps38betgold/1000)*1000;
				
				
				
				if(hgbetgold > hgmaxbetgold) {
					hgbetgold = hgmaxbetgold;
				}
				
				
				if(hgbetgold > BetClient.mainWnd.gethgmaxbet()) {
					hgbetgold = BetClient.mainWnd.gethgmaxbet();
				}
				
				ps38betgold = (hgbetgold/100);
				
			}
			
			
			

			
			if(ps38betgold < Double.parseDouble(ps38oddUpdres.get("minbet"))) {
				ps38betgold = Double.parseDouble(ps38oddUpdres.get("minbet")) + 1.0;
			}
			
			
			
			
			
			SimpleDateFormat dfSec = new SimpleDateFormat("HH:mm:ss");
			
			if(oddUpdres == null || ps38oddUpdres == null) {
				
				System.out.println(dfSec.format(System.currentTimeMillis()) + ":获取赔率失败");
				
				return;
			}
			
			
			String betres = "";

			
			BetAdyData bad = new BetAdyData();
			
			
			
			bad.hgps38 = updownarg;
			
			bad.currenttime = dfSec.format(System.currentTimeMillis());
			
			bad.league = betgame.hgevent.leagueName;
			bad.teams = betgame.hgevent.homeTeam + "-vs-" + betgame.hgevent.awayTeam;
			bad.time = betgame.hgevent.eventTime;
			
			
			bad.score = betgame.hgevent.homeTeamScore + ":" + betgame.hgevent.awayTeamScore;
			
			bad.hgeventid = betgame.hgevent.eventId;
			
			bad.bettype = bettype;
			
			bad.hgbettype = hgTeamlbStr.replace("皇冠:", "") + oddUpdres.get("ratio");
			bad.betpankou = ps38betpankou;
			bad.hgbettime = Long.toString(System.currentTimeMillis());
			
			bad.hgbetres = "";
			
			bad.ticketid = "";
			bad.gid = gidstr;
			bad.hgbetgold = Integer.toString(hgbetgold);
			
			double hgratio1 = hgratio;
			if(hgratio < 0) {
				hgratio1 = 2.0 + hgratio;
			}
			
			double ps38ratio1 = ps38ratio;
			if(ps38ratio< 0) {
				ps38ratio1 = 2.0 + ps38ratio1;
			}
			
			
			bad.mergeratio = String.format("%.3f", hgratio1 + ps38ratio1);
			
			bad.ps38eventid = Long.toString(betgame.event.eventId);
			
			
			bad.wagerId = "";
			bad.ps38betres = "";
			//bad.ps38bettime = dfSec.format(System.currentTimeMillis());
			bad.ps38bettype = ps38Teamlbstr + ps38oddUpdres.get("ratio");
			bad.ps38betgold = Double.toString(ps38betgold);
			
			bad.ps38rebetflag = "0";
			
			
			//todo  对赔率进行分析，看是否还能进行投注
			if(!isInRatioRange(hgratio, ps38ratio)) {
				
				
				betres  = "赔率不符条件";
				
				bad.hgbetres = betres;
				bad.ps38betres = betres;
				
				//发出声音
	    		if(SoundThread.inPlay == false){
					SoundThread st = new SoundThread();
					SoundThread.status = 1;
					st.start();
				}
				
				BetClient.betCtrMng.updateBetLogData(bad);
				
				return;
			}
			

			
			
			if(Math.abs(hgpankou - ps38pankou) > 0.01) {
				
				betres  = "盘口不一致";
				
				bad.hgbetres = betres;
				bad.ps38betres = betres;
				
				//发出声音
	    		if(SoundThread.inPlay == false){    			
					SoundThread st = new SoundThread();
					SoundThread.status = 1;
					st.start();
				}
				
				BetClient.betCtrMng.updateBetLogData(bad);
				
				return;
			}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			//int hgminbetgold = Integer.parseInt(oddUpdres.get("minbet"));
			int hgminbetgold = Integer.parseInt(oddUpdres.get("minbet").replace(",", ""));
			//int hgmaxbetgold = Integer.parseInt(oddUpdres.get("maxbet").replace(",",""));
			
			
			if(hgbetgold < hgminbetgold || hgbetgold > hgmaxbetgold) {
				
				return;
			}
			
			int ps38minbetgold = (int)Double.parseDouble(ps38oddUpdres.get("minbet"));
			int ps38maxbetgold = (int)Double.parseDouble(ps38oddUpdres.get("maxbet"));
			
			if(ps38betgold < ps38minbetgold || ps38betgold > ps38maxbetgold) {
				
				return;
			}
			
			
			
			//sleep
			
			int refreshdelaybettime = BetClient.mainWnd.getrefreshdelaybettime();
			
			if(refreshdelaybettime > 0 ) {
				Thread.sleep(refreshdelaybettime * 1000);
			}
			
			
			
			
			
			
			
			Map<String,String> res = BetClient.betCtrMng.hgdobet(oddUpdres.get("betParam"), hgbetgold);
			
			
			//String[] res = {"1", ""};
			
			if(res == null) {
				System.out.println("auto bet: 皇冠账号尚未登录");
				
				System.out.println(dfSec.format(System.currentTimeMillis())  + ": 皇冠账号尚未登录,投注返回null:" + bad.toString());
				bad.hgbetres = "账号掉线";
				BetClient.betCtrMng.updateBetLogData(bad);
				
				//messagelb.setForeground(new Color(255,0,0));
			}else {
				
				if(res.get("status").equals("1") || res.get("status").equals("0")) {
					

					
					bad.league = betgame.hgevent.leagueName;
					bad.teams = betgame.hgevent.homeTeam + "-vs-" + betgame.hgevent.awayTeam;
					bad.time = betgame.hgevent.eventTime;
					
					
					
					
					bad.hgeventid = betgame.hgevent.eventId;
					
					bad.bettype = bettype;
					
					bad.hgbettype = res.get("betteam") + "@ " + res.get("betratio");
					bad.betpankou = res.get("betpankou");
					bad.hgbettime = bad.hgbettime = Long.toString(System.currentTimeMillis());
					
					bad.hgbetres = res.get("status");
					
					bad.ticketid = res.get("ticketid");
					bad.gid = gidstr;
					bad.hgbetgold = Integer.toString(hgbetgold);
					
					
					
					
					
					double bethgratio = Double.parseDouble(res.get("betratio"));
					if(bethgratio < 0) {
						bethgratio = 2.0 + bethgratio;
					}
					
					
					bad.mergeratio = String.format("%.3f", bethgratio + ps38ratio1);
					

					
					//bad.mergeratio = String.format("%.3f", hgratio + ps38ratio);
					
					//bet ps38
					Map<String, String> rval = BetClient.betCtrMng.ps38dobet(ps38oddUpdres, (int)ps38betgold);
					
					
					
					bad.ps38eventid = Long.toString(betgame.event.eventId);
					
					
					if(rval.containsKey("rebet")) {
						bad.ps38rebetflag = "1";
					}else {
						bad.ps38rebetflag = "0";
					}
					
					
					if(rval.get("status").equals("1")) {

						bad.wagerId = rval.get("wagerId");
						bad.ps38betres = "1";
						//bad.ps38bettime = rval.get("bettime");
						bad.ps38bettype = ps38Teamlbstr + rval.get("ratio");
						bad.ps38betgold = Integer.toString((int)ps38betgold);
						
						double betps38ratio = Double.parseDouble(rval.get("ratio"));
						if(betps38ratio< 0) {
							betps38ratio = 2.0 + betps38ratio;
						}
						
						bad.mergeratio = String.format("%.3f", bethgratio + betps38ratio);
						
						
						if(res.get("status").equals("0")) {
							System.out.println("皇冠投注待确认, ps38投注成功");
						}else {
							System.out.println("皇冠投注成功, ps38投注成功");
							//TODO remove
							//BetClient.betCtrMng.useNextAccount();
						}
						
						
						
						
						
						
						
					}else if(rval.get("status").equals("0")){
						
						bad.wagerId = rval.get("wagerId");
						bad.ps38betres = "0";
						//bad.ps38bettime = rval.get("bettime");
						bad.ps38bettype = ps38Teamlbstr + rval.get("ratio");
						bad.ps38betgold = Integer.toString((int)ps38betgold);
						
						double betps38ratio = Double.parseDouble(rval.get("ratio"));
						if(betps38ratio< 0) {
							betps38ratio = 2.0 + betps38ratio;
						}
						
						bad.mergeratio = String.format("%.3f", bethgratio + betps38ratio);
						
						
						if(res.get("status").equals("0")) {
							System.out.println("皇冠投注待确认, ps38投注待确认");
						}else {
							System.out.println("皇冠投注成功, ps38投注待确认");
						}
						
						
						
					
						
					}else {
						
						if(res.get("status").equals("0")) {
							System.out.println("皇冠投注待确认, ps38投注失败");
						}else {
							System.out.println("皇冠投注成功, ps38投注失败");
						}

						
					}
					
					
					
					
					BetClient.betCtrMng.updateBetAdyData(bad);
					BetClient.betCtrMng.updateBetLogData(bad);
					
					//发出声音
		    		if(SoundThread.inPlay == false){    			
						SoundThread st = new SoundThread();
						SoundThread.status = 0;
						st.start();
					}
					
				}else{
					//System.out.println("皇冠投注失败");
					//发出声音
					
					bad.hgbetres = res.get("status");
					
					BetClient.betCtrMng.updateBetLogData(bad);
		    		if(SoundThread.inPlay == false){    			
						SoundThread st = new SoundThread();
						SoundThread.status = 1;
						st.start();
					}
		    		
		    		if(res.get("status").equals("-4")) {
						//使用下一个特殊账号
						BetClient.betCtrMng.useNextAccount();
					}
					
				}
				
				
				
			}
			
			
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
		}
	}
	
	
	private boolean isInRatioRange(double hgodd, double ps38odd) {
		
		double sideMinodd = BetClient.mainWnd.getsideMinodd();
		double sumMinodd = BetClient.mainWnd.getsumMinodd();
		
		double sideMaxodd = BetClient.mainWnd.getsideMaxodd();
		double sumMaxodd = BetClient.mainWnd.getsumMaxodd();
		
		DecimalFormat df = new DecimalFormat("#.000");
		
		if(hgodd < 0) {
			hgodd = Double.parseDouble(df.format(2.0 + hgodd));
		}
		
		
		
		
		if(ps38odd < 0.0) {
			ps38odd = Double.parseDouble(df.format(2.0 + ps38odd));
		}
		
		if(hgodd + ps38odd < BetClient.mainWnd.getbetodd()) {
			return false;
		}
		
		
		double betodd = BetClient.mainWnd.getbetodd();
		
		if(hgodd >= sideMinodd && ps38odd >= sideMinodd && (hgodd + ps38odd) >= betodd && hgodd <= sideMaxodd && ps38odd <= sideMaxodd && (hgodd+ps38odd) <= sumMaxodd) {
			return true;
		}
		
		return false;
	}
	
}
